package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cderp.mapper.TblShopMapper;
import com.ruoyi.cderp.domain.TblShop;
import com.ruoyi.cderp.service.ITblShopService;

/**
 * 门店Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-10
 */
@Service
public class TblShopServiceImpl implements ITblShopService
{
    @Autowired
    private TblShopMapper tblShopMapper;

    LoginUser loginUser = null;

    /**
     * 查询门店
     *
     * @param id 门店主键
     * @return 门店
     */
    @Override
    public TblShop selectTblShopById(String id)
    {
        return tblShopMapper.selectTblShopById(id);
    }

    /**
     * 查询门店列表
     *
     * @param tblShop 门店
     * @return 门店
     */
    @Override
    public List<TblShop> selectTblShopList(TblShop tblShop)
    {
        return tblShopMapper.selectTblShopList(tblShop);
    }

    /**
     * 新增门店
     *
     * @param tblShop 门店
     * @return 结果
     */
    @Override
    public int insertTblShop(TblShop tblShop)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String formattedDate = sdf.format(new Date());
        Random random = new Random();
        int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
        tblShop.setId("10" + formattedDate+randomNumber);
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        tblShop.setCreateById(loginUser.getUser().getUserId().toString());
        tblShop.setCreateBy(loginUser.getUser().getUserName());
        tblShop.setCreateTime(DateUtils.getNowDate());
        return tblShopMapper.insertTblShop(tblShop);
    }

    /**
     * 修改门店
     *
     * @param tblShop 门店
     * @return 结果
     */
    @Override
    public int updateTblShop(TblShop tblShop)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        tblShop.setUpdateById(loginUser.getUser().getUserId().toString());
        tblShop.setUpdateBy(loginUser.getUser().getUserName());
        tblShop.setUpdateTime(DateUtils.getNowDate());
        return tblShopMapper.updateTblShop(tblShop);
    }

    /**
     * 批量删除门店
     *
     * @param ids 需要删除的门店主键
     * @return 结果
     */
    @Override
    public int deleteTblShopByIds(String[] ids)
    {
        return tblShopMapper.deleteTblShopByIds(ids);
    }

    /**
     * 删除门店信息
     *
     * @param id 门店主键
     * @return 结果
     */
    @Override
    public int deleteTblShopById(String id)
    {
        return tblShopMapper.deleteTblShopById(id);
    }
}
